<?php 
require '/library/config.php';
$host_name = 'http://'.$_SERVER['HTTP_HOST'];
$client_id = '';
$client_key = '';
$access_code = $_REQUEST['access_code'];
$token_url = 'http://id.sudo.vn/oauth/accessCode/'.$access_code.'';
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "{$client_id}:{$client_key}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$respone_data = curl_exec($ch);
//var_dump($respone_data);die;
$info = json_decode(base64_decode($respone_data),true);
//var_dump($info);die;
if($info['status'] == 1) {
   $user = array();
   $user['name'] = substr($info['user']['email'],0,strpos($info['user']['email'],'@'));
   $user['name'] = $user['name'].'_sudo';
   $user['email'] = $info['user']['email'];
   $user['password'] = md5('taitt'.$info['user']['id']);
   $conn=mysql_connect("{$config['db']['host']}", "{$config['db']['username']}", "{$config['db']['password']}") or die("can't connect database");
   mysql_select_db("{$config['db']['dbname']}",$conn);
   $sql="select email from xf_user where email='".$user['email']."' AND username='".$user['name']."'";
   $query=mysql_query($sql);
   if(mysql_num_rows($query) == 1) {
      $str = "<form action='index.php?login/login' method='post' name='frm'>";
      $str .= "<input type='hidden' name='login' value='".$user['name']."'>";
      $str .= "<input type='hidden' name='register' value='0'>";
      $str .= "<input type='hidden' name='password' value='".$user['password']."'>";
      $str .= "<input type='hidden' name='cookie_check' value='1'>";
      $str .= "<input type='hidden' name='redirect' value='/index.php'>";
      $str .= "<input type='hidden' name='_xfToken' value=''>";
      $str .= "</form>";
      $str .= '<script language="JavaScript">document.frm.submit();</script>';
      echo $str;
   }else {
      $str = "<form action='index.php?register/register' method='post' name='frm'>";
      $str .= "<input type='hidden' name='username' value='".$user['name']."'>";
      $str .= "<input type='hidden' name='email' value='".$user['email']."'>";
      $str .= "<input type='hidden' name='password' value='".$user['password']."'>";
      $str .= "<input type='hidden' name='password_confirm' value='".$user['password']."'>";
      $str .= "<input type='hidden' name='dob_month' value='1'>";
      $str .= "<input type='hidden' name='dob_day' value='1'>";
      $str .= "<input type='hidden' name='dob_year' value='1989'>";
      $str .= "<input type='hidden' name='gender' value='male'>";
      $str .= "<input type='hidden' name='timezone' value='Asia/Bangkok'>";
      $str .= "<input type='hidden' name='agree' value='1'>";
      $str .= "<input type='hidden' name='_xfToken' value=''>";
      $str .= "<input type='hidden' name='reg_key' value=''>";
      $str .= "</form>";
      $str .= '<script language="JavaScript">document.frm.submit();</script>';
      echo $str;
   }
   
}else {
   die('Lỗi kết nối đăng nhập với ID Sudo');
}
?>